package com.bwie.myannotationtest.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class MyBaseAdapter<T> extends BaseAdapter {

	private Context context;
	private List<T> list;
	
	public MyBaseAdapter(Context context, List<T> list) {
		super();
		this.context = context;
		this.list = list;
	}

	public Context getContext() {
		return context;
	}

	public List<T> getList() {
		return list;
	}
	
	public void clear(){
		if (list!=null) {
			list.clear();
		}
	}
	
	public void addTopList(List<T> list,boolean isClearOld){
		if (list==null) {
			return;
		}
		if (isClearOld) {
			this.list.clear();
		}
		this.list.addAll(0,list);
	}
	
	public void addButtomList(List<T> list,boolean isClearOld){
		if (list==null) {
			return;
		}
		if (isClearOld) {
			this.list.clear();
		}
		this.list.addAll(list);
	}
	
	public void addObject(T t,boolean isClearOld){
		if (t==null) {
			return;
		}
		if (isClearOld) {
			this.list.clear();
		}
		this.list.add(t);
	}
	
	public void addObject(T t,int index,boolean isClearOld){
		if (t==null) {
			return;
		}
		if (isClearOld) {
			this.list.clear();
		}
		this.list.add(index,t);
	}
	
	public void update(){
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return myView(position,convertView,parent);
	}

	public abstract View myView(int position, View convertView, ViewGroup parent);
}
